<?php
require_once '../system/init.php';

$albums = new Albums();
$album = $albums->editAlbumAction();
$events = new Events();
$eventsList = $events->getEvents(0, 0);

$photos = $albums->getAlbumPhotosFromDb($album['album_id']);
//$pager = $albums->getPhotoPagerHtml($event['event_id']);

//$folders = array_keys($events->getEventsFoldersFromStorage());
//$eventsFolders = $events->getEventsFoldersFromDb();

//$folders = array_diff($folders, $eventsFolders);

require_once 'inc/header.php';

// Страница только для залогиненных пользователей
Common::checkLogin($user);

?>

<div class="main">
    <div class="center">
        <ul class="menu">
            <li><a href="/index.php" class="btn btn-active">Альбомы</a></li>
            <li><a href="/tv/all_events.php" class="btn">События</a></li>
        </ul>

        <div id="user-info" class="h">
            <span id="user-name"></span>
            <span id="user-lastname"></span>
        </div>
    </div>
    <div class="clear"></div>
    <fieldset><legend><h1>Редактирование альбома</h1></legend></fieldset>

<div style="width: 500px; margin: 0 auto;">
    <? if ($albums): ?>
    <form method="post" enctype="multipart/form-data" onsubmit="return checkForm ( );">
        <input type="hidden" name="album_id" value="<?=$album['album_id']?>" />
        <input type="hidden" name="album_folder" value="<?=$album['album_folder']?>" />
        <table class="form" cellpadding="0" cellspacing="0">
            <tr>
                <td class="field-name"><label for="album_name">Название</label></td>
                <td><input type="text" name="album_name" id="album_name" class="w-400" value="<?=$album['album_name']?>" onFocus="hideError(this.id);" /></td>
                <td class="errors" id="album_name_error"></td>
            </tr>
            <tr>
                <td class="field-name"><label for="album_autor">Автор</label></td>
                <td><input type="text" name="album_autor" id="album_autor" class="w-400" value="<?=$album['album_autor'] ?>"/></td>
                <td class="errors"></td>
            </tr>
            <tr>
                <td class="field-name"><label for="album_date">Дата</label></td>
                <td><input type="text" name="album_date" id="album_date" class="w-400 datepicker" value="<?=$album['album_date']?>" /></td>
                <td id="album_date_error" class="errors"></td>
            </tr>
<!--            <tr>-->
<!--                <td class="field-name"><label for="event_desc">Описание</label></td>-->
<!--                <td><textarea id="event_desc" name="event_desc" class="w-400 h-100">--><?//=$event['event_desc']?><!--</textarea></td>-->
<!--                <td class="errors" id="event-desc-error"></td>-->
<!--            </tr>-->
            <tr>
                <td class="field-name">Фотографии</td>
                <td class="middle">
                    <input type="file" value="" name="photos[]" multiple>
                    <span class="relative">Папка c фото: <?=$album['album_folder']?>&nbsp;</span>
                </td>
            </tr>
            <tr>
                <td class="field-name"></td>
                <td><input type="submit" name="submit" class="btn" />&nbsp;&nbsp;<input type="submit" name="delete" class="btn" value="Удалить" /></td>
            </tr>
        </table>
    </form>
    </div><br>
    <? if ($photos) :?>
        <fieldset><legend><h1> Фотографии альбома </h1></legend></fieldset>
        <?//=$pager?>
        <div class="event_photos">
            <? foreach ($photos as $photo): ?>
            <div class="photo photo-<?=$photo['photo_id']?>"  >
                <a href="<?=$photo['photo_path']?>" class="img" rel="prettyPhoto" style="background-image: url('<?=$photo['photo_path']?>'); background-repeat: no-repeat;" title="<?=$photo['photo_desc']?>" ></a>
                <form class="photo-info">
                    <table class="form" cellpadding="0" cellspacing="0">
                        <tr>
                            <td class="field-name"><label class="field-name" for="photo-desc-<?=$photo['photo_id']?>">Описание</label></td>
                            <td><textarea id="<?=$photo['photo_id']?>" name="photo-desc" class="w-400 h-80 photo_desc_input"><?=$photo['photo_desc']?></textarea></td>
                        </tr>
                        <tr>
                            <td class="field-name"></td>
                            <td><label for="event_attach_<?=$photo['photo_id']?>">Привязать к событию:</label>
                                <select name="event_attach_<?=$photo['photo_id']?>" id="event_attach_<?=$photo['photo_id']?>" onchange="attachToEvent('<?=$photo['photo_id']?>', this);" style='width:290px; margin-left: 3px;'>
                                    <option value="" value='0'>--</option>
                                    <? foreach ($eventsList as $event): ?>
                                    <option <?if ($event['event_id'] == $photo['event_id']) :?> selected="selected" <?endif?> value="<?=$event['event_id']?>"><?=$event['event_name']?></option>
                                    <? endforeach ?>
                                </select>
                            </td>

                        </tr>
                        <tr>
                            <td class="field-name"></td>
                            <td>
                                <input type="button" class="btn photo-info-save" value="Удалить" onclick="del(<?=$photo['photo_id']?>, '<?=$photo['photo_path']?>' );"/>
                                <em class="em" style="display: none; top: -85px; "></em>
                            </td>
                        </tr>
                    </table>
                </form>

            </div>
            <? endforeach ?>
        </div>
<!--        <a class="btn btn-up" href="#"> Наверх </a>-->
<!--        --><?//=$pager?>
        <? endif ?>
    <? endif ?>
</div>


<script>
    $(document).ready(function(){
        $("a[rel^='prettyPhoto']").prettyPhoto();
    });
</script>



<script>
    function attachToEvent(idPhoto, element) {

        var params = {id:idPhoto, ajax:1, attach:1, eventId:$(element).val()};

        params = $.param(params);

        $.post("ajax_photoInfoEdit.php", params , function (data) {
            if (data.message) {
                $('.photo-' + id).find(".em").html(data.message).animate({opacity: "show", top: "-65"}, 500);
                $('.photo-' + id).find(".em").animate({opacity: "hide", top: "-65"}, 1500);
            }
        });
    }

    $(document).ready(function(){

        $('.photo_desc_input').change(function(){
            var id = $(this).attr('id');
            var val = $(this).val();

            var params = {id:id, ajax:1, val:val, desc:1};

            params = $.param(params);

            $.post("ajax_photoInfoEdit.php", params , function (data) {
                if (data.message) {
                    $('.photo-' + id).find(".em").html(data.message).animate({opacity: "show", top: "-65"}, 500);
                    $('.photo-' + id).find(".em").animate({opacity: "hide", top: "-65"}, 1500);
                }
            });
        })



    });


    function del (id, path){

        var params = {id:id, ajax:1, path:path};
        params = $.param(params)

        $.post("ajax_photoDelete.php", params , function (data) {
            if (data.message) {
               // alert(data.message);
                $('body').find('.photo-' + id).animate({ backgroundColor: "#fbc7c7" }, "fast")
                    .animate({ opacity: "hide" }, "slow")
            } else {
                alert(data.message);
            }
        });
    }




    function checkForm() {
        return true;
        var nameEvent = $("#event_name");

        if (nameEvent.val()) {
            return true;
        } else {
            nameEvent.css('border','1px #ff4040 solid');
            $("#event_name_error").text('введите название события');
            return false;
        }
    }


    function hideError(inputFieldId) {
        $("#"+inputFieldId+"-error").text('');
        $("#"+inputFieldId).css('border','1px #ccc solid');
    }

</script>

<script>
    $(function(){
        $.datepicker.setDefaults(
            $.extend($.datepicker.regional["ru"])
        );
        $(".datepicker").datepicker({
            dateFormat:'yy-mm-dd'
        });
    });

</script>
<? require_once 'inc/footer.php'; ?>